package com.omenkk.juc.execute;

import com.omenkk.juc.util.Slepper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.BlockingQueue;
@Slf4j
public class demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         *     public static ExecutorService newFixedThreadPool(int nThreads) {
         *         return new ThreadPoolExecutor(nThreads, nThreads,
         *                                       0L, TimeUnit.MILLISECONDS,
         *                                       new LinkedBlockingQueue<Runnable>());
         *核心线程数等于最大线程数，因此无需超时时间
         * 阻塞队列无界，可以放任意数量的任务
         */
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Task task1=new Task();
        Task task2=new Task();
        Task task3=new Task();
        //执行任务
        executor.execute(task1);
        executor.execute(task2);
        executor.execute(task3);
        //submit适用于需要返回值的任务
        String str = "ok";
        Future<String> res = executor.submit(task1, str);
        System.out.println(res.get());
        FutureTask<String> task=new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "abc";
            }
        });
executor.shutdownNow();
executor.shutdown();
    }
}
    @Slf4j
class Task implements Runnable{
    @Override
    public void run() {
        log.debug("execute测试...执行任务");
        Slepper.sleep(3);

    }
}

